//
//  DetailsViewController.m
//  oschina_song
//
//  Created by song on 16/1/31.
//  Copyright © 2016年 song. All rights reserved.
//

#import "DetailsViewController.h"
#import "OSCNews.h"
#import "OSCNewsDetails.h"
#import "OSCBlogsDetails.h"
#import "OSCBlogs.h"
#import "OSCAPI.h"
#import "AFNetworking.h"
#import "AFHTTPRequestOperationManager+Util.h"
#import "OSCSoftwareDetails.h"

@interface DetailsViewController()

@property(nonatomic, strong) UIWebView *detailView;

@property(nonatomic, copy) NSString *detailsURL;

@property(nonatomic, copy) NSString *tag;

@property(nonatomic ,assign) SEL loadMethod;

@property(nonatomic , assign) Class detailsClass;

@end


@implementation DetailsViewController


-(instancetype)initWithNews:(OSCNews *)news
{
    self = [super initWithModeSwitchButton:YES];
    
    if(self)
    {
         self.hidesBottomBarWhenPushed = YES;
  
        switch (news.type) {
            case NewsTypeStandardNews:
                self.navigationItem.title= @"咨询详情";
                _tag = @"news";
                _detailsURL = [NSString stringWithFormat:@"%@%@?id=%lld",OSCAPI_PREFIX, OSCAPI_NEWS_DETAIL,news.newsID];
                _detailsClass = [OSCNewsDetails class];
                _loadMethod = @selector(loadNewsDetails:);
                break;
            case NewsTypeSoftWare:
                self.navigationItem.title= @"软件详情";
                _tag =@"software";
                _detailsClass = [OSCSoftwareDetails class];
                _loadMethod =@selector(loadSoftDetails:);
                _detailsURL = [NSString stringWithFormat:@"%@%@?ident=%@",OSCAPI_PREFIX, OSCAPI_SOFTWARE_DETAIL,news.attachment];
                break;
            default:
                break;
        }
    }
    
    return self;
}

-(id)initWithBlog:(OSCBlogs *)blogs
{
    self = [super init];
    
    if(self){
        _detailsURL =[NSString stringWithFormat:@"%@%@?id=%lld",OSCAPI_PREFIX ,OSCAPI_BLOG_DETAIL , blogs.blogID];
        _tag = @"blog";
        self.hidesBottomBarWhenPushed = YES;
        self.navigationItem.title = @"博客详情";
        _detailsClass = [OSCBlogsDetails class];
        _loadMethod = @selector(loadBlogDetails:);
        
        
    }
    return self;
}



-(void)viewDidLoad
{
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    _detailView = [UIWebView new];
    _detailView.delegate = self;
//    _detailView.hidden = YES;
    [self.view addSubview:_detailView];
    [self.view bringSubviewToFront:(UIView *) self.editingBar];

    
    for (UIView *view in self.view.subviews) {view.translatesAutoresizingMaskIntoConstraints = NO;}
    
    NSDictionary *views = @{@"_detailView":_detailView ,@"_editingbar":self.editingBar};
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_detailView]|" options:0 metrics:nil views:views]];
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_detailView][_editingbar]|" options:NSLayoutFormatAlignAllLeft|NSLayoutFormatAlignAllRight metrics:nil views:views]];
    
    [self.editingBar.modeSwitchButton sendActionsForControlEvents:UIControlEventTouchUpInside];
    [self fetchDetails];
}

-(void)fetchDetails
{
    AFHTTPRequestOperationManager *manger =[ AFHTTPRequestOperationManager OSCManger];
    
    [manger GET:_detailsURL parameters:nil success:^(AFHTTPRequestOperation *  operation, ONOXMLDocument  *responseDocument) {
        id details;
        ONOXMLElement *XML =[responseDocument.rootElement firstChildWithTag:_tag];
        details =[[_detailsClass alloc] initWithXML:XML];
        [self performSelector:_loadMethod withObject:details];
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        
        
    }];
}

-(void)loadNewsDetails:(OSCNewsDetails *)details
{
    [self.detailView loadHTMLString:details.html baseURL:[[NSBundle mainBundle] resourceURL]];
}

-(void)loadSoftDetails:(OSCSoftwareDetails *)details
{
     [self.detailView loadHTMLString:details.html baseURL:[[NSBundle mainBundle] resourceURL]];
}
-(void)loadBlogDetails:(OSCBlogsDetails *)details
{
    [self.detailView loadHTMLString:details.html baseURL:[[NSBundle mainBundle] resourceURL]];
}

@end
